//
//  ACPlayBackMainViewController.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/7/8.
//

#import "ACPlayBackMainViewController.h"
#import "JXCategoryView.h"
#import "ACLiveIntroductionViewController.h"
#import "ACLiveFileListViewController.h"
#import "TXVodPlayer.h"
#import "ACPlayBackBarView.h"
#import <YKWoodpecker.h>

@interface ACPlayBackMainViewController ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate,TXVodPlayListener,YKWPluginProtocol>
{
    UIImageView *topBgView;
    UIButton *closeBtn;
    
}
@property(nonatomic,strong)ACLiveModel *liveModel;
@property (nonatomic, strong) JXCategoryTitleView *myCategoryView;
@property (nonatomic, strong) JXCategoryBaseView *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, strong) NSArray *titles;
@property(nonatomic,assign)NSInteger selectedIndex;
@property(nonatomic,strong)ACLiveFileListViewController *filelistVC;
@property(nonatomic,strong)UILabel *liveTitle;
@property(nonatomic,strong)UIView *videoView;
@property(nonatomic,strong)TXVodPlayer *player;
@property(nonatomic,strong)ACPlayBackBarView *barView;
@end

@implementation ACPlayBackMainViewController
-(TXVodPlayer *)player{
    if (!_player) {
        _player = [[TXVodPlayer alloc] init];
        TXVodPlayConfig *config = [[TXVodPlayConfig alloc] init];
        config.enableAccurateSeek = YES;
        config.maxBufferSize = 2;
        config.progressInterval = 0.1;
        config.smoothSwitchBitrate = YES;
        config.playerType = PLAYER_FFPLAY;
        _player.config = config;
        _player.vodDelegate = self;
        _player.enableHWAcceleration = YES;
        [_player setRenderMode:RENDER_MODE_FILL_EDGE];
    }
    return _player;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.categoryView];
    [self.view addSubview:self.listContainerView];
    
    self.filelistVC = [[ACLiveFileListViewController alloc] init];
    self.filelistVC.liveID = self.liveID;
    
    self.videoView = [[UIView alloc] init];
    [self.view addSubview:self.videoView];
    [self.videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ACStatusBarHeight);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(ACSCREEN_WIDTH * 9.0 / 16.0);
    }];
    
    [self.player setupVideoWidget:self.videoView insertIndex:0];
    [self.player startPlay:@"http://htsec-demo-1302545507.cos.ap-nanjing.myqcloud.com/video/gje2fkgb8cctslguuh3c/57d4e4d224c2c60460dfc6744bd6bef6.mp4"];
    
    [self.player currentPlaybackTime]; // 当前播放时间
    [self.player playableDuration]; // 可播放时长
    
    topBgView = [[UIImageView alloc]initWithImage: [UIImage imageWithName:@"icon_live_room_bg" bundle:ACLiteAVUISDK targetClass:[self class]]];
    topBgView.userInteractionEnabled = YES;
    [self.view addSubview:topBgView];
    [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(48 + ACStatusBarHeight);
    }];
    
    self.barView = [[ACPlayBackBarView alloc] init];
    [self.view addSubview:self.barView];
    [self.barView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(48);
        make.bottom.equalTo(self.videoView.mas_bottom);
    }];
    
    self.liveTitle = [[UILabel alloc] init];
    self.liveTitle.text = @"";
    self.liveTitle.textAlignment = NSTextAlignmentCenter;
    self.liveTitle.textColor = UIColorFromRGB(0xffffff);
    self.liveTitle.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [topBgView addSubview:self.liveTitle];
    [self.liveTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(topBgView.mas_bottom).offset(-10);
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-60);
    }];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageWithName:@"icon_live_room_close" bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateNormal];
    [self.view addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(28, 28));
        make.bottom.equalTo(topBgView.mas_bottom).offset(-10);
        make.right.equalTo(topBgView.mas_right).offset(-15);
    }];
    @weakify(self)
    [[closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self.navigationController popViewControllerAnimated:YES];
        if (self.player.isPlaying) {
            [self.player stopPlay];
        }
        [self.player removeVideoWidget];
    }];
    
    self.fd_prefersNavigationBarHidden = YES;
    self.fd_interactivePopMaxAllowedInitialDistanceToLeftEdge = 200;
    
    self.titles = [[NSArray alloc] initWithObjects:@"简介",@"文档", nil];
    self.myCategoryView.titles = self.titles;
    self.myCategoryView.titleColor = UIColorFromRGB(0x909399);
    self.myCategoryView.titleSelectedColor = UIColorFromRGB(0xFF6048);
    self.myCategoryView.titleFont = [UIFont systemFontOfSize:14];
    self.myCategoryView.titleSelectedFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    self.myCategoryView.titleLabelMaskEnabled = YES;
    self.myCategoryView.defaultSelectedIndex = 0;
    
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorWidth = JXCategoryViewAutomaticDimension;
    self.myCategoryView.indicators = @[lineView];
    self.myCategoryView.titleColorGradientEnabled = YES;
    [self.myCategoryView removeFromSuperview];
   
    [self.view addSubview:self.myCategoryView];
    [self.myCategoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(ACSCREEN_WIDTH * 9.0 / 16.0 + ACStatusBarHeight);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = UIColorFromRGB(0xE2E1E0);
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.categoryView.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.top.equalTo(line.mas_bottom);
    }];
}
- (void)requestLiveInfo{
    [SVProgressHUD showWithStatus:@""];
    [ACLiveRoomManager requestLiveDetailWithLiveID:self.liveID success:^(id  _Nonnull responseObject) {
        self.liveModel = [ACLiveModel mj_objectWithKeyValues:responseObject];
        self.liveTitle.text = self.liveModel.title;
        [SVProgressHUD dismiss];
        NSDictionary *dic = [self.listContainerView validListDict];
        ACLiveIntroductionViewController *introductionVC = [dic objectForKey:[NSNumber numberWithInteger:0]];
        introductionVC.liveModel = self.liveModel;
    } failure:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}
#pragma mark - TXVodPlayListener
-(void)onPlayEvent:(TXVodPlayer *)player event:(int)EvtID withParam:(NSDictionary *)param{
    
}
-(void)onNetStatus:(TXVodPlayer *)player withParam:(NSDictionary *)param{
    
}
#pragma mark - JXCategoryViewDelegate
// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    DDLogInfo(@"%@", NSStringFromSelector(_cmd));
    // 侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
}

// 滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    DDLogInfo(@"%@", NSStringFromSelector(_cmd));
}

#pragma mark - JXCategoryListContainerViewDelegate
// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}

// 返回各个列表菜单下的实例，该实例需要遵守并实现 <JXCategoryListContentViewDelegate> 协议
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    if (index == 0) {
        NSBundle *curB = [NSBundle BundleWithBundleName:ACLiteAVUISDK targetClass:[self class] ];
        ACLiveIntroductionViewController *VC = [[ACLiveIntroductionViewController alloc] initWithNibName:@"ACLiveIntroductionViewController" bundle:curB];
        return VC;
    }else{
        self.filelistVC.index = 2;
        return self.filelistVC;
    }
}
// 分页菜单视图
- (JXCategoryBaseView *)categoryView {
    if (!_categoryView) {
        _categoryView = [self preferredCategoryView];
        _categoryView.delegate = self;
        _categoryView.listContainer = self.listContainerView;
    }
    return _categoryView;
}
-(JXCategoryTitleView *)myCategoryView{
    return (JXCategoryTitleView *)self.categoryView;;
}
- (JXCategoryBaseView *)preferredCategoryView {
    return [[JXCategoryTitleView alloc] init];
}
// 列表容器视图
- (JXCategoryListContainerView *)listContainerView {
    if (!_listContainerView) {
        _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    }
    return _listContainerView;
}

- (void)runWithParameters:(NSDictionary *)paraDic {
    [UIApplication sharedApplication].delegate.window.rootViewController = self;
}


@end
